package com.meihua.shellapp.activity.wallet;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.dialog.AlertDialog;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.CustomerKeyboard;
import com.meihua.shellapp.view.PasswordEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

import static com.meihua.shellapp.R.id.iv_top_left;

/**
 * 申请提现界面
 */
public class ApplyWithdrawwalsActivity extends BaseActivity implements CustomerKeyboard.CustomerKeyboardClickListener, PasswordEditText.PasswordFullListener {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(iv_top_left)
    ImageView mIVTopLeft;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_bank)
    TextView tvBank;
    @Bind(R.id.tv_money)
    TextView tvMoney;
    @Bind(R.id.tv_time)
    TextView tvTime;
    private PasswordEditText passwordEditText;
    private CustomerKeyboard mCustomerKeyboard;
    private String bank_card;
    private String money;
    private AlertDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_apply_withdrawwals;
    }

    /*初始化View*/
    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("申请提现");
        mIVTopLeft.setVisibility(View.VISIBLE);
        bank_card = getIntent().getStringExtra("bank_card");/*卡号*/
        String bank_name = getIntent().getStringExtra("bank_name");/*卡名称*/
        money = getIntent().getStringExtra("money");/*提现金额*/
        tvName.setText(MyApplication.getInstance().bean.getName());
        tvBank.setText(bank_name);
        tvMoney.setText(money);
    }

    @OnClick({R.id.iv_top_left, R.id.withdrawal_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_left:
                finish();
                break;
            case R.id.withdrawal_btn_next:
               /*弹出输入密码对话框*/
                showCustomInputDialog();
                break;
        }
    }

    /*弹出输入密码对话框*/
    private void showCustomInputDialog() {

        //弹出dialog输入框
        dialog = new AlertDialog.Builder(this).
                setContentView(R.layout.activity_password).
                fullWith().
                setCancelable(true).
                formBottom(true).
                show();
        passwordEditText = dialog.getView(R.id.password);
        mCustomerKeyboard = dialog.getView(R.id.customer_keyboard);
        mCustomerKeyboard.setOnCustomerKeyboardClickListener(this);
        passwordEditText.setEnabled(false);
        passwordEditText.setOnPasswordFullListener(this);
    }

    @Override
    public void click(String numder) {
        passwordEditText.addPassword(numder);
    }

    @Override
    public void delete() {
        passwordEditText.deleteLastPassword();
    }

    @Override
    public void passwordFull(final String password) {
        /*验证支付密码*/
        Map<String, String> params = new HashMap() {{
            put("pay_password", password);
            put("m_number", MyApplication.getInstance().getUserId());
        }};
        NetHelper.post(URL.getUrl().VERIFICATIONPAYPWD, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(response.body());
                    if (obj.getInt("code") == 1) {
                        confirmWithdraw();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(ApplyWithdrawwalsActivity.this, "支付密码验证失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, true);
    }

    /*确认提现*/
    private void confirmWithdraw() {
        Map<String, String> params = new HashMap() {{
            put("m_number", MyApplication.getInstance().getUserId());
            put("account_number", bank_card);
            put("money", money);
        }};
        NetHelper.post(URL.getUrl().CASHCONFIRM, params, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(Response<String> response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(response.body());
                    if (obj.getInt("code") == 1) {
                        Toast.makeText(ApplyWithdrawwalsActivity.this, "提现成功", Toast.LENGTH_SHORT).show();
                        setResult(11);
                        finish();
                    } else {
                        Toast.makeText(ApplyWithdrawwalsActivity.this, obj.getString("msg"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, true);
    }

    @Override
    protected void onDestroy() {
        if (dialog != null) dialog.cancel();
        super.onDestroy();
    }
}
